package com.tarzan.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tarzan.edu.dao.EduChapterMapper;
import com.tarzan.edu.entity.EduChapter;
import com.tarzan.edu.entity.EduVideo;
import com.tarzan.edu.entity.response.ChapterVo;
import com.tarzan.edu.entity.response.EduChapterResp;
import com.tarzan.edu.entity.response.VideoVo;
import com.tarzan.edu.service.IEduChapterService;
import com.tarzan.edu.service.IEduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liujinshan
 * @title: EduChapterServiceImpl
 * @projectName com.tarzan.edu.service.impl
 * @description: TODO
 * @date 2021/5/25 16:56
 */
@Service
public class EduChapterServiceImpl  extends ServiceImpl<EduChapterMapper, EduChapter> implements IEduChapterService {

    @Autowired
    private IEduVideoService iEduVideoService;//注入小节service

    @Override
    public void insert(EduChapter eduChapter) {
        baseMapper.insert(eduChapter);
    }

    @Override
    public void delete(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void update(EduChapter eduChapter) {
        baseMapper.updateById(eduChapter);
    }

    @Override
    public EduChapterResp findTeacherById(String id) {
        EduChapterResp resp = new EduChapterResp();
        EduChapter eduChapter = baseMapper.selectById(id);
        BeanUtils.copyProperties(eduChapter,resp);
        return resp;
    }

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1 根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //2 根据课程id查询课程里面所有的小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = iEduVideoService.list(wrapperVideo);

        //创建list集合，用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();

        //3 遍历查询章节list集合进行封装
        //遍历查询章节list集合
        for (int i = 0; i < eduChapterList.size(); i++) {
            //每个章节
            EduChapter eduChapter = eduChapterList.get(i);
            //eduChapter对象值复制到ChapterVo里面
            ChapterVo chapterVo = new ChapterVo();
            chapterVo.setId(eduChapter.getId().toString());
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //把chapterVo放到最终list集合
            finalList.add(chapterVo);

            //创建集合，用于封装章节的小节
            List<VideoVo> videoList = new ArrayList<>();

            //4 遍历查询小节list集合，进行封装
            for (int m = 0; m < eduVideoList.size(); m++) {
                //得到每个小节
                EduVideo eduVideo = eduVideoList.get(m);
                //判断：小节里面chapterid和章节里面id是否一样
                if(eduVideo.getChapterId().equals(eduChapter.getId())) {
                    //进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //放到小节封装集合
                    videoList.add(videoVo);
                }
            }
            //把封装之后小节list集合，放到章节对象里面
            chapterVo.setChildren(videoList);
        }
        return finalList;
    }
}
